<template>
  <div>
    <a-button @click="getNC"> getNC </a-button>
    <!-- <div>NC: {{ records }}</div> -->

    <!--  
    :customRow="tableCustomRow"
      :scroll="heightS" -->

    <a-table :row-selection="rowSelection" :columns="columns" :data-source="records" :row-key="record => record.id">
      <template slot="action" slot-scope="text,record">
        <router-link :to="{
          path: '/financial/invoiceList/invoiceDetail',
          query: {
            invoiceId: record.id
          }
        }" @click.native="saveId(record.id)">
          维护客户
        </router-link>
        <!-- 这个动作要跳转到客户管理页面，完成客户资料维护过后需要有按钮调回本页 -->

        <a-divider type="vertical" />
        
        <a @click.prevent="deleteAlert(record.id)">维护商品</a>
        <!-- 这个动作要跳转到商品信息管理页面，完成客户资料维护过后需要有按钮调回本页 -->


      </template>
    </a-table>


    <a-button @click="checkNC"> checkNC </a-button>

    <a-button @click="selectSome"> selectSome </a-button>
    <!-- 这个按钮应该是判断有选中的数据后，切换到下一个ods组件 -->
  </div>
</template>

<script>
import api from '@/odoorpc'
// import VueDraggableResizable From 'vue-draggable-resizable'

export default {
  name: 'NcTreeView',
  components: {},
  mixins: [],
  props: {},

  data() {
    return {
      records: [],
      activeIds: []
    }
  },

  computed: {
    // 这里的数据要可以筛选，进页面的时候根据登录用户的关联关系筛选，后端需要增加用于筛选的字段，可以先随便找一个字段进行筛选（如company_code = 1109）
    columns() {
      return [
        { dataIndex: 'invoice_num', key: 'invoice_num', title: 'NC发票号', ellipsis: true },
        { dataIndex: 'company_name', key: 'company_name', title: '公司名称', ellipsis: true },
        { dataIndex: 'partner_code', key: 'partner_code', title: '客户代码', ellipsis: true },
        { dataIndex: 'partner_name', key: 'partner_name', title: '客户名称', ellipsis: true },
        { dataIndex: 'amount_total', key: 'amount_total', title: '总金额', ellipsis: true },
        { dataIndex: 'create_date', key: 'create_date', title: '创建时间', ellipsis: true },
        { dataIndex: 'api_status', key: 'api_status', title: 'NC状态', ellipsis: true },
        { dataIndex: 'ods_order_id', key: 'ods_order_id', title: '订单号', ellipsis: true },
        { dataIndex: 'state', key: 'state', title: '状态', ellipsis: true },
        { dataIndex: 'name', key: 'name2', title: 'NC发票PK', ellipsis: true },
        { dataIndex: 'biz_type', key: 'biz_type', title: 'NC类型', ellipsis: true },
        { dataIndex: 'company_code', key: 'company_code', title: '公司代码', ellipsis: true },

        { dataIndex: 'action',key: 'action',title: '操作',scopedSlots: { customRender: 'action' } }
      ]
    },

    rowSelection() {
      return {
        selectedRowKeys: this.activeIds,
        onChange: (selectedRowKeys, selectedRows) => {
          // console.log(selectedRowKeys, selectedRows)
          this.activeIds = selectedRowKeys
          // this.handleOnRowSelect(selectedRowKeys, selectedRows)
        }
      }
    }
  },

  created() { },

  mounted() {
    this.getNC()
    // console.log('mounted', this.$route.fullPath)
  },

  methods: {
    async getNC() {
      const Model = api.env.model('fp.nc.ods.sale.order')
      const domain = []
      const fields = [
        'ods_order_id',
        'state',
        'name',
        'invoice_num',
        'biz_type',
        'company_code',
        'company_name',
        'partner_code',
        'partner_name',
        'amount_total',
        'api_status',
        'create_date'
      ]

      const records = await Model.search_read({ domain, fields })
      this.records = records


    },

    // 这个check动作放在后端完成，odoo需要维护代码
    async checkNC() {

      const codes = this.records
        .filter(item => this.activeIds.includes(item.id))
        .map(item => item.partner_code)
      const Model = api.env.model('res.partner')
      const domain = [['ref', 'in', codes]]
      const fields = ['ref']
      const ptns = await Model.search_read({ domain, fields })
      console.log(ptns, codes)
      // 检查是不是少了
      // if 少了 应该去 打开 客户编辑页面
      // else ok
    },

    selectSome() {
      this.$emit('select', this.activeIds)
    },

    deleteAlert(id) {
      if (confirm("确定删除吗？")) {
        console.log('-- 确定删除吗？ --', id);
        this.deleteOne(id);
      }
    },

    deleteOne(v){
      console.log('-- 删除 --',v);
    },

    saveId(v){
      console.log('-- saveId --',v);
      // localStorage.setItem('invoiceId',v);
    }
  }
}
</script>

<style type="text/css">

</style>
